<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="故障码" prop="errorCode">
        <el-input
          v-model="queryParams.errorCode"
          placeholder="请输入故障码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="条件类型" prop="conType">
        <el-select v-model="queryParams.conType" placeholder="请选择条件类型 0:先决条件，1:判断条件" clearable>
          <el-option
            v-for="dict in dict.type.con_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:conditions:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:conditions:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:conditions:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:conditions:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="conditionsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="80" align="center"/>
      <!--也可以优化为位置-->
      <el-table-column label="故障码" align="center" prop="errorCode"/>
      <el-table-column label="条件类型" align="center" prop="conType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.con_type" :value="scope.row.conType"/>
        </template>
      </el-table-column>
      <el-table-column label="字段1" align="center" prop="field1">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.con_field" :value="scope.row.field1"/>
        </template>
      </el-table-column>
      <el-table-column label="操作符1" align="center" prop="operation1">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.con_oper" :value="scope.row.operation1"/>
        </template>
      </el-table-column>
      <el-table-column label="值1" align="center" prop="value1"/>
      <el-table-column label="字段2" align="center" prop="field2">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.con_field" :value="scope.row.field2"/>
        </template>
      </el-table-column>
      <el-table-column label="操作符2" align="center" prop="operation2">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.con_oper" :value="scope.row.operation2"/>
        </template>
      </el-table-column>
      <el-table-column label="值2" align="center" prop="value2"/>
      <el-table-column label="异常信息" align="center" prop="errorInfo"/>
      <el-table-column label="异常可能原因" align="center" prop="errorReason"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:conditions:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:conditions:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改故障条件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--优化为故障位置-->
        <el-form-item label="故障id" prop="errorId">
          <el-input v-model="form.errorId" placeholder="请输入故障id"/>
        </el-form-item>
        <el-form-item label="故障码" prop="errorCode">
          <el-input v-model="form.errorCode" placeholder="请输入故障码"/>
        </el-form-item>
        <el-form-item label="条件类型" prop="conType">
          <el-select v-model="form.conType" placeholder="请选择条件类型 0:先决条件，1:判断条件">
            <el-option
              v-for="dict in dict.type.con_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="条件所需字段1" prop="field1">
          <el-select v-model="form.field1" placeholder="请选择条件所需字段1">
            <el-option
              v-for="dict in dict.type.con_field"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作符1" prop="operation1">
          <el-select v-model="form.operation1" placeholder="请选择操作符1">
            <el-option
              v-for="dict in dict.type.con_oper"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="条件对应字段1" prop="value1">
          <el-input v-model="form.value1" placeholder="请输入条件对应字段1"/>
        </el-form-item>
        <el-form-item label="条件所需字段2" prop="field2">
          <el-select v-model="form.field2" placeholder="请选择条件所需字段2">
            <el-option
              v-for="dict in dict.type.con_field"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作符2" prop="operation2">
          <el-select v-model="form.operation2" placeholder="请选择操作符2">
            <el-option
              v-for="dict in dict.type.con_oper"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="条件对应字段2" prop="value2">
          <el-input v-model="form.value2" placeholder="请输入条件对应字段2"/>
        </el-form-item>
        <el-form-item label="异常信息" prop="errorInfo">
          <el-input v-model="form.errorInfo" placeholder="请输入异常信息"/>
        </el-form-item>
        <el-form-item label="异常可能原因" prop="errorReason">
          <el-input v-model="form.errorReason" placeholder="请输入异常可能原因"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listConditions, getConditions, delConditions, addConditions, updateConditions} from "@/api/system/conditions";

  export default {
    name: "Conditions",
    dicts: ['con_field', 'con_oper', 'con_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 故障条件表格数据
        conditionsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          errorId: null,
          errorCode: null,
          conType: null,
          field1: null,
          operation1: null,
          value1: null,
          field2: null,
          operation2: null,
          value2: null,
          errorInfo: null,
          errorReason: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询故障条件列表 */
      getList() {
        this.loading = true;
        listConditions(this.queryParams).then(response => {
          this.conditionsList = response.rows;
          console.log(this.conditionsList);
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          conId: null,
          errorId: null,
          errorCode: null,
          conType: null,
          field1: null,
          operation1: null,
          value1: null,
          field2: null,
          operation2: null,
          value2: null,
          errorInfo: null,
          errorReason: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.conId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加故障条件";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const conId = row.conId || this.ids
        getConditions(conId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改故障条件";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.conId != null) {
              updateConditions(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addConditions(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const conIds = row.conId || this.ids;
        this.$modal.confirm('是否确认删除故障条件编号为"' + conIds + '"的数据项？').then(function () {
          return delConditions(conIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/conditions/export', {
          ...this.queryParams
        }, `conditions_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
